package logic

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"pharymacy-management-system/dao/mysql"
	"pharymacy-management-system/models"
	"pharymacy-management-system/network"
	"pharymacy-management-system/pkg/snowflake"
	"strconv"
)

func AddSaleStatus(c *gin.Context) {
	var statusName string
	statusName = c.Param("statusName")
	salesStatus := models.SalesStatus{
		StatusID:   0,
		StatusName: statusName,
	}
	if salesStatusIsHas(&salesStatus) {
		network.ResponseError(c, network.CodeInvalidParam)
		return
	}

	err := mysql.InsertStatus(statusName)
	if err != nil {
		zap.L().Error("mysql.InsertStatus(statusName) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}

	network.ResponseSuccess(c, nil)
}

func salesStatusIsHas(salesStatus *models.SalesStatus) bool {
	var err error
	if !(salesStatus.StatusName == "" || len(salesStatus.StatusName) == 0) {
		salesStatus, err = mysql.SelectStatusByStatusName(salesStatus.StatusName)
	} else {
		salesStatus, err = mysql.SelectStatusByStatusID(salesStatus.StatusID)
	}
	if err != nil {
		return false
	}
	if salesStatus != nil {
		return true
	}
	return false
}

func UpdateSaleStatus(c *gin.Context) {
	var newSalesStatus models.ParamSalesStatus
	err := c.ShouldBindJSON(&newSalesStatus)
	if err != nil {
		zap.L().Error("c.ShouldBindJSON(&salesStatus) failed", zap.Error(err))
		network.ResponseError(c, network.CodeInvalidParam)
		return
	}

	var salesStatus = models.SalesStatus{
		StatusID:   newSalesStatus.SalesStatusID,
		StatusName: newSalesStatus.NewSalesStatusName,
	}

	if salesStatusIsHas(&salesStatus) {
		network.ResponseError(c, network.CodeInvalidParam)
		return
	}

	err = mysql.UpdateStatus(salesStatus.StatusID, salesStatus.StatusName)
	if err != nil {
		zap.L().Error("mysql.UpdateStatus(salesStatus.StatusID, salesStatus.StatusName) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}

	network.ResponseSuccess(c, nil)
}

func GetAllSaleStatus(c *gin.Context) {
	saleStatusList, err := mysql.SelectAllStatus()
	if err != nil {
		zap.L().Error("mysql.SelectAllStatus() failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	data := map[string]interface{}{}
	data["statusList"] = saleStatusList
	network.ResponseSuccess(c, data)
}

func AddSales(c *gin.Context) {
	userId, err := network.GetCurrentUser(c)
	if err != nil {
		zap.L().Error("network.GetCurrentUser(c) failed", zap.Error(err))
		network.ResponseError(c, network.CodeNeedLogin)
		return
	}

	sales := &models.Sales{
		OrderID:      snowflake.GenID(),
		StatusID:     1,
		CreateUserID: userId,
	}

	err = mysql.CreateOrder(sales)
	if err != nil {
		zap.L().Error("mysql.CreateOrder(sales) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	network.ResponseSuccess(c, nil)
}

func ApproveSales(c *gin.Context) {
	var orderID int64
	var statusID int
	// 将string类型强制转换为int
	orderID, _ = strconv.ParseInt(c.Param("orderID"), 10, 64)
	statusID, _ = strconv.Atoi(c.Param("statusID"))
	userId, err := network.GetCurrentUser(c)
	if err != nil {
		zap.L().Error("network.GetCurrentUser(c) failed", zap.Error(err))
		network.ResponseError(c, network.CodeNeedLogin)
		return
	}

	sales, err := mysql.SelectOrderByID(orderID)
	if err != nil {
		zap.L().Error("mysql.SelectOrderByID(orderID) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	sales.OrderID = orderID
	sales.StatusID = statusID
	sales.ApproveUserID = userId

	err = mysql.UpdateOrderByID(sales)
	if err != nil {
		zap.L().Error("mysql.UpdateOrderByID(sales) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}

	network.ResponseSuccess(c, nil)
}
func ExecSales(c *gin.Context) {
	var orderID int64
	var statusID int
	// 将string类型强制转换为int
	orderID, _ = strconv.ParseInt(c.Param("orderID"), 10, 64)
	statusID, _ = strconv.Atoi(c.Param("statusID"))
	userId, err := network.GetCurrentUser(c)
	if err != nil {
		zap.L().Error("network.GetCurrentUser(c) failed", zap.Error(err))
		network.ResponseError(c, network.CodeNeedLogin)
		return
	}

	sales, err := mysql.SelectOrderByID(orderID)
	if err != nil {
		zap.L().Error("mysql.SelectOrderByID(orderID) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	sales.OrderID = orderID
	sales.StatusID = statusID
	sales.ExecUserID = userId

	err = mysql.UpdateOrderByID(sales)
	if err != nil {
		zap.L().Error("mysql.UpdateOrderByID(sales) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}

	network.ResponseSuccess(c, nil)
}
func SettlementSales(c *gin.Context) {
	var orderID int64
	var statusID int
	// 将string类型强制转换为int
	orderID, _ = strconv.ParseInt(c.Param("orderID"), 10, 64)
	statusID, _ = strconv.Atoi(c.Param("statusID"))
	userId, err := network.GetCurrentUser(c)
	if err != nil {
		zap.L().Error("network.GetCurrentUser(c) failed", zap.Error(err))
		network.ResponseError(c, network.CodeNeedLogin)
		return
	}

	sales, err := mysql.SelectOrderByID(orderID)
	if err != nil {
		zap.L().Error("mysql.SelectOrderByID(orderID) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	sales.OrderID = orderID
	sales.StatusID = statusID
	sales.SettlementUserID = userId

	err = mysql.UpdateOrderByID(sales)
	if err != nil {
		zap.L().Error("mysql.UpdateOrderByID(sales) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}

	network.ResponseSuccess(c, nil)
}

func GetAllSaleByStatus(c *gin.Context) {
	statusID, err := strconv.Atoi(c.Param("statusID"))
	if err != nil {
		zap.L().Error("strconv.Atoi(c.Param(\"statusID\")) failed", zap.Error(err))
		network.ResponseError(c, network.CodeInvalidParam)
		return
	}

	orderList, err := mysql.SelectOrderListByStatusID(statusID)
	if err != nil {
		zap.L().Error("mysql.SelectOrderListByStatusID(statusID) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}

	data := map[string]interface{}{}
	data["orderList"] = orderList
	network.ResponseSuccess(c, data)
}

func GetSalesByOrderID(c *gin.Context) {
	orderID, _ := strconv.ParseInt(c.Param("orderID"), 10, 64)
	order, err := mysql.SelectOrderByID(orderID)
	if err != nil {
		zap.L().Error("mysql.SelectOrderByID(orderID) failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	data := map[string]interface{}{}
	data["order"] = order
	network.ResponseSuccess(c, data)
}
